﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Globalization;

namespace PromotionEngine
{
    public class Response
    {
        public List<Model.Customer> Data { get; set; }
        public int Count { get; set; }
        public string Errors { get; set; }

        public Response(List<Model.Customer> data, int count)
        {
            this.Data = data;
            this.Count = count;
        }

        public Response(string errors)
        {
            this.Errors = errors;
        }

        public Response()
        {

        }
    }
    
    public partial class Customer : System.Web.UI.Page
    {
        public string sysFormat = "";
        protected void Page_Load(object sender, EventArgs e)
        {
             sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetODataCustomerList1(string keySearch, string keyWords)
        {
            Promotion pmt = new Promotion();
            if (keySearch.Trim().Length == 0 && keyWords.Trim().Length == 0)
            {
                long count = pmt.GetCountCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
                long maximunDisplay;
                long.TryParse(System.Configuration.ConfigurationManager.AppSettings["MaximunDisplayItems"].ToString(), out maximunDisplay);
                if (count <= maximunDisplay)
                {
                    DataSet dsPromotion = pmt.GetAllCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                        bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
                    if (dsPromotion != null)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                if (keySearch.Trim().Length > 0)
                {
                    DataSet dsPromotion = pmt.GetCustomerByCode(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(), keySearch);
                    if (dsPromotion != null)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
                else if (keyWords.Trim().Length > 0)
                {
                    DataSet dsPromotion = pmt.GetCustomersForSearch(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                 keyWords);
                    if (dsPromotion != null)
                    {
                        return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
                    }
                }
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetODataCustomerList()
        {
            List<Model.Customer> lstCustomer = new List<Model.Customer>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.Customer customer = new Model.Customer();
                    customer.CompanyCode = r["CompanyCode"].ToString();
                    customer.CustomerCode = r["CustomerCode"].ToString();
                    customer.CustomerName = r["CustomerName"].ToString();
                    customer.CustomerID = r["CustomerID"].ToString();
                    customer.CompanyName = r["CompanyName"].ToString();
                    customer.BillingAddress = r["BillingAddress"].ToString();
                    customer.ShippingAddress = r["ShippingAddress"].ToString();
                    customer.PhoneNumber = r["PhoneNumber"].ToString();
                    customer.ContactPerson = r["ContactPerson"].ToString();
                    customer.Email = r["Email"].ToString();
                    customer.IsActive = bool.Parse(r["IsActive"].ToString());
                    customer.GradeID = r["GradeID"].ToString();
                    customer.GradeName = r["GradeName"].ToString();
                    if (r["TotalPoint"].ToString().Length > 0)
                    {
                        customer.TotalPoint = double.Parse(r["TotalPoint"].ToString());
                    }
                    if (r["ExpiredPoint"].ToString().Length > 0)
                    {
                        customer.ExpiredPoint = double.Parse(r["ExpiredPoint"].ToString());
                    }
                    if (r["UsedPoint"].ToString().Length > 0)
                    {
                        customer.UsedPoint = double.Parse(r["UsedPoint"].ToString());
                    }
                    if (r["AvailablePoint"].ToString().Length > 0)
                    {
                        customer.AvailablePoint = double.Parse(r["AvailablePoint"].ToString());
                    }
                    if (r["Birthday"].ToString().Length > 0)
                    {
                        customer.Birthday = DateTime.Parse(r["Birthday"].ToString());
                    }
                    else
                    {
                        customer.Birthday = DateTime.Now;
                    }
                    if (r["SpecialDay"].ToString().Length > 0)
                    {
                        customer.SpecialDay = DateTime.Parse(r["SpecialDay"].ToString());
                    }
                    else
                    {
                        customer.SpecialDay = DateTime.Now;
                    }
                    lstCustomer.Add(customer);
                }
                return new Response(lstCustomer, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static List<Model.Customer> GetODataCustomers()
        {
            List<Model.Customer> lstCompany = new List<Model.Customer>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null && dsPromotion.Tables.Count>0)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.Customer customer = new Model.Customer();
                    customer.CompanyCode = r["CompanyCode"].ToString();
                    customer.CustomerCode = r["CustomerCode"].ToString();
                    customer.CustomerName = r["CustomerName"].ToString();
                    customer.CustomerID = r["CustomerID"].ToString();
                    customer.IsActive = bool.Parse(r["IsActive"].ToString());
                    lstCompany.Add(customer);
                }
                
            }
            return lstCompany;
        }
        [WebMethod]
        public static string GetCompanys()
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCompany(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString());
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateCustomer(string oCustomer, bool isInsert)
        {
            Model.Customer customer = JsonConvert.DeserializeObject<Model.Customer>(oCustomer);
            if (isInsert)
            {
                customer.CreatedDate = DateTime.Now;

                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        customer.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        customer.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();

            string errMsg = pmt.UpdateJsonCustomer(customer.CustomerID, customer.CustomerCode, customer.CustomerName, customer.CompanyCode, customer.CreatedUserID, customer.CreatedDate
                , customer.IsActive, customer.BillingAddress, customer.ShippingAddress, customer.ContactPerson, customer.PhoneNumber, customer.Email, 
                     customer.Birthday,customer.SpecialDay,customer.GradeID, ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {

            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetCustomers()
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomer(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
    }
}